package com.shujia.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;

public class hadoopAPI {
    FileSystem fs;
    @Before
   public void init()throws Exception{
        Configuration conf = new Configuration ( );
        conf.set ("dfs.replication","1");
        URI uri = new URI ("hdfs://master:9000");
         fs = FileSystem.get (uri, conf);
    }
    @After
    public void close()throws Exception{
        fs.close ();
    }
    @Test
    public void mkdir()throws Exception{
        fs.mkdirs (new Path ("/mk"));
    }
    @Test
    public void delete()throws Exception{
       fs.delete (new Path ("/text.txt"),true);
    }
    @Test
    public void list()throws Exception{
        FileStatus[] fileStatuses = fs.listStatus (new Path ("/"));
        for (FileStatus fileStatus : fileStatuses) {
            System.out.println (fileStatus.getLen ( ));

        }
    }
    @Test
    public void listfile()throws Exception{
        FileStatus fileStatus = fs.getFileStatus (new Path ("/diba.txt"));
        System.out.println (fileStatus );


    }
    @Test
    public void lord()throws Exception{
        FSDataInputStream open = fs.open (new Path ("/data/diba1.txt"));
        BufferedReader br = new BufferedReader (new InputStreamReader (open));
        String len;
        while((len=br.readLine ())!=null){
            System.out.println (len );
        }
        br.close ();
        open.close ();


    }
    @Test
    public void create()throws Exception{
        FSDataOutputStream fsDataOutputStream = fs.create (new Path ("/text.txt"));
        BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (fsDataOutputStream));
        bw.write ("你好");
        bw.write ("wrold");
        bw.write ("你妈的");
        bw.close ();
        fsDataOutputStream.close ();
    }
    @Test
    public void copyFromLocalFile()throws Exception{
        fs.copyFromLocalFile (new Path ("H:\\soft\\IDEA1.8\\bd13\\diba.txt"),new Path ("/"));
    }
    @Test
    public void copyToLocalFile()throws Exception{
        fs.copyToLocalFile (true,new Path ("/data/diba1.txt"),new Path ("H:\\soft\\IDEA1.8\\bd13"),true);
    }

}
